/**
 * 
 */
package com.gr.staffpm.pages.schedule;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.pages.schedule.model.Event;


/**
 * @author Graham Rhodes 31 Mar 2011 14:50:18
 */
public class WeekViewEvent extends Panel {

    private static final long serialVersionUID = 1L;
    private final static int PIXELS_IN_HALF_HOUR = 20;
    private final static int PIXELS_IN_HOUR = 42;
    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Event event;
    private final int top, height;
    private int width = 100, left = 0;

    public WeekViewEvent(String id, Event event) {
        super(id);
        this.event = event;

        int hoursSinceMidnight = 0;
        int halfHoursSpanned = 1;
        //Normalise times?
        DateTime eventStart = event.getStart(), eventEnd = event.getEnd();

        if (eventEnd.isBefore(eventStart.plusMinutes(30))) {
            eventEnd = event.getEnd().withTime(event.getEnd().getHourOfDay(), 0, 0, 0);
        }

        Period p = new Period(eventStart.withTime(0, 0, 0, 0), eventStart, PeriodType.time());
        hoursSinceMidnight = p.getHours();
        top = (hoursSinceMidnight * PIXELS_IN_HOUR) + ((p.getMinutes() / 30) * PIXELS_IN_HALF_HOUR);
        p = new Period(eventStart, eventEnd, PeriodType.minutes());
        halfHoursSpanned = (p.getMinutes() / 30);
        height = halfHoursSpanned * PIXELS_IN_HALF_HOUR;

        Label title = new Label("title", event.getTitle());
        title.setVisible(!event.isEmpty());
        add(title);
    }

    @Override
    protected void onComponentTag(ComponentTag tag) {
        tag.getAttributes().put("style", "left: " + left + "%; width: " + width + "%; top: " + top + "px; height: " + height + "px;");
        tag.getAttributes().put("class", "ext-color-" + event.getCalendar().getId() + " ext-cal-evt ext-cal-evr");
    }

    public Event getEvent() {
        return event;
    }

    public boolean isContainedWithin(WeekViewEvent weekEvent) {
        boolean endsIn = !event.getStart().isAfter(weekEvent.getEvent().getStart()) && event.getEnd().isAfter(weekEvent.getEvent().getStart());
        boolean startsEndsIn = !event.getStart().isBefore(weekEvent.getEvent().getStart()) && !event.getEnd().isAfter(weekEvent.getEvent().getEnd());
        boolean startsIn = !event.getStart().isAfter(weekEvent.getEvent().getEnd()) && event.getEnd().isAfter(weekEvent.getEvent().getEnd());
        return endsIn || startsEndsIn || startsIn;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
        add(new AttributeModifier("style", new StringModel("left: " + left + "%; width: " + width + "%; top: " + top + "px; height: " + height + "px;")));
    }

    public int getLeft() {
        return left;
    }

    public void setLeft(int left) {
        this.left = left;
        add(new AttributeModifier("style", new StringModel("left: " + left + "%; width: " + width + "%; top: " + top + "px; height: " + height + "px;")));
    }

}
